package com.aidex.biz.controller;

import com.aidex.biz.domain.*;
import com.aidex.biz.service.*;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.Company;
import com.aidex.common.core.domain.entity.SysRole;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作台
 * 按企业简介、消息待办、省市中央新闻、项目预警布局分析
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/biz/worksIndex")
public class WorksIndexController extends BaseController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private FriendLinkService friendLinkService;

    @Autowired
    private DaibanIngService daibanIngService;

    @Autowired
    private BizAssessToDeptService bizAssessToDeptService;

    @Autowired
    private MonthDeptService monthDeptService;

    @Autowired
    private YearDeptService yearDeptService;

    @Autowired
    private LinshiDeptManageService linshiDeptManageService;

    @Autowired
    private BizAssessInfoService bizAssessInfoService;

    @Autowired
    private BizAnhuanCompanyService bizAnhuanCompanyService;

    /**
     * 查询新闻管理列表
     */
    @GetMapping("/works")
    public R works() {
        Company company = companyService.get("admin123");
        List<FriendLink> friendLinks = friendLinkService.findList(new FriendLink());
        Map<String, Object> map = new HashMap<>();

        List<DaibanIng> daibanIngs = new ArrayList<>();
        System.out.println(SecurityUtils.getLoginUser().getUser().getRole());
        SysRole role = SecurityUtils.getLoginUser().getUser().getSysRoles().size() > 0 ?
                SecurityUtils.getLoginUser().getUser().getSysRoles().get(0) : null;
        if (role != null && StringUtils.isNotNull(role.getRoleKey())) {
            // 集团
            if (role.getRoleKey().equals("jjdept")) {
                BizAssessInfo bizAssessInfo = new BizAssessInfo();
                bizAssessInfo.setDeptId(SecurityUtils.getDeptId());
                bizAssessInfo.setStatus("1");
                List<BizAssessInfo> bizAssessInfos = bizAssessInfoService.findList(bizAssessInfo);
                for (BizAssessInfo bizAssess : bizAssessInfos) {
                    DaibanIng daibanIng = new DaibanIng();
                    daibanIng.setContent(bizAssess.getTitle());
                    daibanIng.setType("考核评分");
                    daibanIng.setRouter("day_assess/assessInfoPingFen");
                    daibanIng.setPath("biz/bizassesstodept/assessInfoPingFen");
                    daibanIngs.add(daibanIng);
                }
            }
            // 子公司
            //日常督办、月度考核、年度考核、临时督办
            if (role.getRoleKey().equals("conghedept")) {
                //日常督办、月度考核、年度考核、临时督办
                BizAssessToDept bizAssessToDept = new BizAssessToDept();
                bizAssessToDept.setDeptId(SecurityUtils.getDeptId());
                bizAssessToDept.setStatus("1");
                List<BizAssessToDept> bizAssessToDepts = bizAssessToDeptService.findList(bizAssessToDept);
                for (BizAssessToDept bizAssess : bizAssessToDepts) {
                    DaibanIng daibanIng = new DaibanIng();
                    daibanIng.setContent(bizAssess.getAssessTitle());
                    daibanIng.setType("日常考核");
                    daibanIng.setRouter("day_assess/myAssessInfo");
                    daibanIng.setPath("biz/bizassesstodept/myAssessInfo");
                    daibanIngs.add(daibanIng);
                }
                MonthDept monthDept = new MonthDept();
                monthDept.setDeptId(SecurityUtils.getDeptId());
                monthDept.setStatus("0");
                List<MonthDept> monthDepts = monthDeptService.findListOrderBy(monthDept);
                for (MonthDept monthDept1 : monthDepts) {
                    DaibanIng daibanIng = new DaibanIng();

                    daibanIng.setContent(monthDept1.getName());
                    daibanIng.setType("年度月报");
                    daibanIng.setDeptId(monthDept.getDeptId());
                    daibanIng.setRouter("baobiao/bizMonthDeptList");
                    daibanIng.setPath("biz/monthdeptlist/index");
                    daibanIngs.add(daibanIng);
                }
                YearDept yearDept = new YearDept();
                yearDept.setDeptId(SecurityUtils.getDeptId());
                yearDept.setStatus("0");
                List<YearDept> yearDepts = yearDeptService.findList(yearDept);
                for (YearDept yearDept1 : yearDepts) {
                    DaibanIng daibanIng = new DaibanIng();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                    daibanIng.setContent("(" + sdf.format(yearDept1.getTimes()) + ")" + "半年总结");
                    daibanIng.setType("半年总结");
                    daibanIng.setRouter("baobiao/bizYearDept");
                    daibanIng.setPath("biz/yeardept/index");
                    daibanIngs.add(daibanIng);

                    DaibanIng daibanIng1 = new DaibanIng();
                    daibanIng1.setContent("(" + sdf.format(yearDept1.getTimes()) + ")" + "年度总结");
                    daibanIng1.setType("年度总结");
                    daibanIng1.setRouter("baobiao/bizYearDept");
                    daibanIng1.setPath("biz/yeardept/index");
                    daibanIngs.add(daibanIng1);
                }

                LinshiDeptManage linshiDeptManage = new LinshiDeptManage();
                linshiDeptManage.setDeptId(SecurityUtils.getDeptId());
                linshiDeptManage.setStatus("1");
                List<LinshiDeptManage> linshiDeptManages = linshiDeptManageService.findList(linshiDeptManage);
                for (LinshiDeptManage linshiDeptManage1 : linshiDeptManages) {
                    DaibanIng daibanIng = new DaibanIng();
                    daibanIng.setContent(linshiDeptManage1.getTitle());
                    daibanIng.setType("临时督办");
                    daibanIng.setRouter("baobiao/bizLinshiDeptManage");
                    daibanIng.setPath("biz/linshideptmanage/index");
                    daibanIngs.add(daibanIng);
                }
            }

        }
        map.put("company", company);
        map.put("friendLinks", friendLinks);
        map.put("daibanIngs", daibanIngs);
        map.put("role", role);
        return R.data(map);
    }

    /**
     * 查询新闻管理列表
     */
    @GetMapping("/anHuanPaiMing")
    public R anHuanPaiMing() {
        return R.data(bizAnhuanCompanyService.findList(new BizAnhuanCompany()));
    }
}
